
###############################################################################
### Did 3G Make Afghan Insurgents Fight More Effectively? 
### Replication files for analyses
### Mehmet Erdem Arslan
###
### Analyses
### Matched wake analyses (Main Text and the Online Appendix)
###############################################################################

# setwd("your file path here")

options(java.parameters = "-Xmx16g")
library(mwa) # requires a few other packages (cem, tcltk, lattice, rJava, MASS)


load("R/mwa_data.Rdata")
# mwa1: for hypothesis 1; all enemy actions including IEDs (195789 events)
# mwa2: for hypothesis 2: IED attacks only (72947 events)
# mwa3: for robustness check: friendly actions (9862 events)

# ...p3: for placebo tests (3 months before treatment timing)
# ...p6: for placebo tests (6 months before treatment timing)

# Functions for presenting mwa results (plot and table)
source("R/Custom_functions_for_mwa.R")

# mwa.R script (https://github.com/kdonnay/mwa/blob/master/R/mwa.R)
# modified to include cem arguments when calculating imbalance 
# specifically, "breaks = ceminputs$L1.breaks" argument is added to the imbalance function at lines 539,541,608,610
source("R/mwa_modified.R")

### Specifying parameters for MWA ###
# 30 to 90 days in steps of 15
t_window <- c(30, 90, 15)
# 4 to 20 kilometers in steps of 4
spat_window <- c(4, 20, 4)
# column and entries that indicate treatment events 
treatment <- c("type","treatment")
# column and entries that indicate control events 
control  <- c("type","control")
# column and entries that indicate dependent events 
dependent <- c("type","dependent")
# columns to match on
matchColumns <- c("troops", "pop_1km", "elev_1km", "nl_1km", "dist")
# use weighted regression
weighted <- TRUE
# temporal units
t_unit <- "days" 
# match on counts of previous treatment and control events
TCM <- TRUE
# formula
frm <- "dependent_post - dependent_pre ~ treatment"


set.seed(123456)
### generating wakes to calculate dependent trend, SO_pre and MO_pre
dat <- slidingWake(mwa1, t_unit = t_unit, t_window = t_window, spat_window = spat_window,
                   treatment = treatment, control = control, dependent = dependent,
                   matchColumns = matchColumns, estimationControls = NULL)

### Calculating L1 profile
imb <- L1.profile(group = dat$treat, dat, max.cut = 20, plot = FALSE, M = 250, 
                  drop = c("eventID", "t_window", "spat_window", "treatment", 
                           "dependent_pre", "dependent_post", "SO_post", "MO_post"))


####### MAIN TEXT #######

### Dependent variable: Insurgent attacks
results <- matchedwake(mwa1, t_window, spat_window, treatment, control, dependent, 
                       matchColumns, weighted = weighted, t_unit = t_unit, TCM = TCM, 
                       cem.method = "maximum", cem.k2k = TRUE, alpha1 = 0.1, 
                       formula = frm, match.details = TRUE, 
                       cem.L1.breaks = imb$medianCP) 

### Dependent variable: IED attacks 
ied.results <- matchedwake(mwa2, t_window, spat_window, treatment, control, dependent,
                           matchColumns, weighted = weighted, t_unit = t_unit, TCM = TCM, 
                           cem.method = "maximum", cem.k2k = TRUE, alpha1 = 0.1, 
                           formula = frm, match.details = TRUE, 
                           cem.L1.breaks = imb$medianCP)

### Figure 3: Empirical results of matched wake analysis
my.mwa.plot(results)
my.mwa.plot(ied.results)

### Table 2: Overview of the Matched Wake Analysis Results
large_table(results, caption = 'Overview of the Matched Wake Analysis Results', label = 'mwa table')


####### PLACEBO TESTS #######

# DV: Insurgent Attacks -- 6 months before treatment timing 
results.placebo6 <- matchedwake(mwa1.p6, t_window, spat_window, 
                                treatment, control, dependent,
                                cem.method = "maximum", alpha1 = 0.1, 
                                cem.k2k = TRUE, match.details = TRUE, 
                                matchColumns, weighted = weighted, 
                                t_unit = t_unit, TCM = TCM, formula = frm,
                                cem.L1.breaks = imb$medianCP)

# DV: Insurgent Attacks -- 3 months before treatment timing 
results.placebo3 <- matchedwake(mwa1.p3, t_window, spat_window, 
                                treatment, control, dependent,
                                cem.method = "maximum", alpha1 = 0.1, 
                                cem.k2k = TRUE, match.details = TRUE, 
                                matchColumns, weighted = weighted, 
                                t_unit = t_unit, TCM = TCM, formula = frm, 
                                cem.L1.breaks = imb$medianCP)

# DV: IED Attacks -- 6 months before treatment timing 
ied.results.placebo6 <- matchedwake(mwa2.p6, t_window, spat_window, 
                                    treatment, control, dependent,
                                    cem.method = "maximum", alpha1 = 0.1,
                                    cem.k2k = TRUE, match.details = TRUE,
                                    matchColumns, weighted = weighted, 
                                    t_unit = t_unit, TCM = TCM, formula = frm, 
                                    cem.L1.breaks = imb$medianCP)

# DV: IED Attacks -- 3 months before treatment timing 
ied.results.placebo3 <- matchedwake(mwa2.p3, t_window, spat_window, 
                                    treatment, control, dependent,
                                    cem.method = "maximum", alpha1 = 0.1,
                                    cem.k2k = TRUE, match.details = TRUE,
                                    matchColumns, weighted = weighted,
                                    t_unit = t_unit, TCM = TCM, formula = frm,
                                    cem.L1.breaks = imb$medianCP)

### Figure 4: Placebo tests for H1 and H2 (Matched wake analysis)
my.mwa.plot(results.placebo3)
my.mwa.plot(results.placebo6)
my.mwa.plot(ied.results.placebo3)
my.mwa.plot(ied.results.placebo6)


####### ONLINE APPENDICES #######

### Appendix A: Detailed results for H2 (IED attacks)
# Table 4: Overview of the Matched Wake Analysis Results with IED Attacks as the Dependent Variable
large_table(ied.results, caption = 'Overview of the Matched Wake Analysis Results with IED Attacks as the Dependent Variable', label = 'mwa H2 IED table')

### Appendix D: Detailed Results of Placebo Tests
# Table 7: Overview of the Placebo Test for H1: Installations of 3G Towers 3 Months Before the Original Date
large_table(results.placebo3, caption = ' ', label = ' ')

# Table 8: Overview of the Placebo Test for H1: Installations of 3G Towers 6 Months Before the Original Date
large_table(results.placebo6, caption = ' ', label = ' ')

# Table 9: Overview of the Placebo Test for H2: Installations of 3G Towers 3 Months Before the Original Date
large_table(ied.results.placebo3, caption = ' ', label = ' ')

# Table 10: Overview of the Placebo Test for H2: Installations of 3G Towers 6 Months Before the Original Date
large_table(ied.results.placebo6, caption = ' ', label = ' ')

### Appendix F: Effects of 3G on Attacks by Government Forces

# Dependent variable: Attacks by Government Forces 
govt.results <- matchedwake(mwa3, t_window, spat_window, treatment, control, dependent,
                            cem.method = "maximum", cem.k2k = TRUE, alpha1 = 0.1,  
                            matchColumns, weighted = weighted, t_unit = t_unit, 
                            match.details = TRUE, TCM = TCM, formula = frm, 
                            cem.L1.breaks = imb$medianCP)


# Placebo test -- 6 months before treatment timing 
govt.results.placebo6 <- matchedwake(mwa3.p6, t_window, spat_window, 
                                     treatment, control, dependent,
                                     cem.method = "maximum", alpha1 = 0.1, 
                                     cem.k2k = TRUE, match.details = TRUE,
                                     matchColumns, weighted = weighted, 
                                     t_unit = t_unit, TCM = TCM, formula = frm, 
                                     cem.L1.breaks = imb$medianCP)

# Placebo test -- 3 months before treatment timing 
govt.results.placebo3 <- matchedwake(mwa3.p3, t_window, spat_window, 
                                     treatment, control, dependent,
                                     cem.method = "maximum", alpha1 = 0.1,
                                     cem.k2k = TRUE, match.details = TRUE,
                                     matchColumns, weighted = weighted, 
                                     t_unit = t_unit, TCM = TCM, formula = frm,
                                     cem.L1.breaks = imb$medianCP)


### Figure 8: Effects of 3G Network on the Attacks by Government Forces
my.mwa.plot(govt.results)
my.mwa.plot(govt.results.placebo3)
my.mwa.plot(govt.results.placebo6)

# Table 12: Overview of the Results with Government Attacks as the Dependent Variable
govt <- govt.results$estimates[ , c(1:5) ]
govt2 <- govt.results.placebo3$estimates[ , c(1:5) ]
govt3 <- govt.results.placebo6$estimates[ , c(1:5) ]

govt <- merge(govt, govt2, by = c('t_window', 'spat_window'), all = TRUE)
govt <- merge(govt, govt3, by = c('t_window', 'spat_window'), all = TRUE)
govt <- arrange(govt, t_window, spat_window)
xtable(govt, caption = 'Overview of the Results with Government Attacks as the Dependent Variable (Matched Wake Analysis)', 
       label = 'mwa table government with placebos', digits = 2)


### Appendix I: Results including covariates in DD regression

results1 <- matchedwake(mwa1, t_window, spat_window, treatment, control, dependent,
                        matchColumns, weighted = weighted, t_unit = t_unit, TCM = TCM,
                        cem.method = "maximum", alpha1 = 0.1, cem.k2k = TRUE, 
                        estimationControls = matchColumns, 
                        cem.L1.breaks = imb$medianCP)

# Figure 9: Results of matched wake analysis (with covariates included in DD regression)
my.mwa.plot(results1)

# Table 15: Overview of the Matched Wake Analysis Results (with covariates included in DD regression)
large_table(results1, caption = ' ', label = ' ')

### Appendix J: Results excluding Kabul City
mwa.k <- mwa1[mwa1$id != 353, ]
results2 <- matchedwake(mwa.k, t_window, spat_window, treatment, control, dependent,
                        matchColumns, weighted = weighted, alpha1 = 0.1, 
                        cem.L1.breaks = imb$medianCP, t_unit = t_unit, 
                        TCM = TCM, formula = frm) # cem.k2k matching is not used here since it only leaves a few observations   

# Figure 10: Results of matched wake analysis (excluding Kabul City)
my.mwa.plot(results2)


rm(list = ls())
##### END OF SCRPIRT #####

